<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/6/29
 * Time: 8:20
 */
namespace app\shopping\controller;

use app\common\model\Base;
use app\shopping\model\Error;
use app\shopping\model\OrderManager;
use app\shopping\model\UserAddress;

class Order extends Base{
    private $orderManager;
    public function __construct()
    {
        parent::__construct();
        $this->orderManager = new OrderManager();
    }
    /**
     *
     * @api {get} /order/  获取商品订单
     * @apiDescription 获取商品订单
     * @apiGroup shopping
     * @apiVersion 0.1.0
     * @apiParam {string} [page] 获取第几页数据每页10个数据
     * @apiParam {number} [state] 获取状态 1：待付款，2：待发货或已付款，3：待收货，4：交易成功，5：交易取消，6：交易关闭,7：待处理,8:已处理,9：已退款
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} count 总数量.
     * @apiSuccess {String} msg 消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function index(){

        $page = empty($_GET["page"])?0:$_GET["page"];
        $indent_state = empty($_GET["state"])?0:$_GET["state"];

        $arr = array(
            "code"=>"1",
            "count"=>$this->orderManager->getStateCount($indent_state),
            "data"=>$this->orderManager->getAllOrders($page,$indent_state),
        );

        echo json_encode($arr);
    }

    public function create(){
        $uid = $_GET["uid"];
        if(empty($uid)){
            return Error::missUid();
        }

        //$address = new UserAddress($_GET["name"],$_GET["address"],$_GET["detailed_address"],$_GET["tel"]);

        //$orderManager = new OrderManager();
        //$orderManager->createOrder($uid,$address);
    }

    /**
     *
     * @api {get} /order/:id  读取某一个订单
     * @apiDescription 获取商品订单
     * @apiGroup shopping
     * @apiVersion 0.1.0
     *

     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function read($id){
//        echo $id;
//        exit();

        if($result=$this->orderManager->getOrderById($id))
            echoJson(1, $result);
        else
            echoJson(0, "数据为空");
    }

    /**
     *
     * @api {put} /order/:id  更新订单状态
     * @apiDescription 接口1：待付款 不能升级 2：待发货或已付款   只能2->3,3->4,4以后就没法用接口变化了
     * @apiGroup shopping
     * @apiVersion 0.1.0
     * @apiParam {string} id 订单号
     * @apiParam {string} state 1：待付款，2：待发货或已付款，3：待收货，4：交易成功，5：交易取消，6：交易关闭,7：待处理,8:已处理,9：已退款
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function update($id){
        if($this->orderManager->updateOrder($id)){
            echoJson(1, "更新成功");
        }else{
            echoJson(0, "更新失败");
        }
    }
}